package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.result.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exception.MyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author kabuda
 * @create 2023-04-24 14:14
 */
@Slf4j
@Api(tags = "讲师接口")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation("获取所有讲师信息")
    @GetMapping("list")
    public R getAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation("删除讲师信息")
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable("id") String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("分页条件查询")
    @PostMapping("/pageCondition")
    public R pageCondition(@RequestBody(required = false) TeacherQuery teacherQuery) {
        HashMap<String, Object> map= this.eduTeacherService.pageCondition(teacherQuery);
        return R.ok().data(map);
    }

    @ApiOperation("根据id查询讲师信息")
    @GetMapping("{id}")
    public R getTeacherById(@PathVariable("id")String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }
    @ApiOperation("修改讲师信息")
    @PutMapping()
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        return flag?R.ok():R.error();
    }
    @ApiOperation("添加讲师信息")
    @PostMapping()
    public R saveTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        return flag?R.ok():R.error();
    }
}
